package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageListBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.List;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/09/22
 * desc   : 状态数据列表
 */
public final class LinkageAdapter extends AppAdapter<LinkageListBean> {

    public LinkageAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    @Override
    public void setData(@Nullable List<LinkageListBean> data) {
        super.setData(data);
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvName;
        private final SwitchButton btnSwitch;
        private final TextView tvDeviceNum;
        private final RecyclerView conditionIconRecyclerView;
        private final RecyclerView actionIconRecyclerView;

        private ViewHolder() {
            super(R.layout.item_linkage);
            tvName = findViewById(R.id.tv_name);
            btnSwitch = findViewById(R.id.btn_switch);
            tvDeviceNum = findViewById(R.id.tv_device_num);
            conditionIconRecyclerView = findViewById(R.id.condition_icon_recycler_view);
            actionIconRecyclerView = findViewById(R.id.action_icon_recycler_view);
        }

        @Override
        public void onBindView(int position) {
            LinkageListBean linkage = getItem(position);
            btnSwitch.setChecked(!linkage.isDisable());
            tvName.setText(linkage.getName());
            tvDeviceNum.setText(getString(R.string.device_num_holder, linkage.getTaskDeviceCount()));
            if(conditionIconRecyclerView.getLayoutManager() == null){
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                conditionIconRecyclerView.setLayoutManager(linearLayoutManager);
            }
            if(actionIconRecyclerView.getLayoutManager() == null){
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
                actionIconRecyclerView.setLayoutManager(linearLayoutManager);
            }
            LinkageConditionIconAdapter conditionIconAdapter = (LinkageConditionIconAdapter) conditionIconRecyclerView.getAdapter();
            LinkageTaskIconAdapter actionIconAdapter = (LinkageTaskIconAdapter) actionIconRecyclerView.getAdapter();
            if(conditionIconAdapter == null){
                conditionIconAdapter = new LinkageConditionIconAdapter(getContext());
                conditionIconRecyclerView.setAdapter(conditionIconAdapter);
            }
            conditionIconAdapter.setData(linkage.getConditionIcons());
            if(actionIconAdapter == null){
                actionIconAdapter = new LinkageTaskIconAdapter(getContext());
                actionIconRecyclerView.setAdapter(actionIconAdapter);
            }
            actionIconAdapter.setData(linkage.getTaskIcons());
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}